/**
 * 学生查看自己的报名状况
 * 
 * /student/topic/detail
 */


const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Topics = require('../../../models/researchTopics')
const User_Info = require('../../../models/usersInfo')
const Topic_Student = require('../../../models/researchTopicStudents')


const get_details = async function(req, res) {
    
    try {
        // 判断学生是否在科研学生列表里
        var topic_student_list = await Topic_Student.findOne({
            where: {
                student_user_name: req.user_name
            }
        })

        if (topic_student_list == null) {
            $.sendMessage(res, 0000, '喔喔！靓仔你不在该年度里喔，不要搞我（学校）的系统喔喔')
            return
        }

        // 获取学生基本信息 专业，班级
        var student_info = await User_Info.findOne({
            where: {
                user_name: req.user_name
            }
        })

        // 当前选题情况
        var student_topic_info = await Topic_Student.findOne({
            where: {
                student_user_name: req.user_name
            }    
        })

        var topic_info = {}
        // 如果学生有报名或者报名成功某个题目，则返回题目信息
        if (student_topic_info.dataValues.student_status != '1') {
            var result_of_topic = await Topics.findOne({
                where: {
                    id: student_topic_info.dataValues.topic_id
                }
            })
            topic_info = result_of_topic.dataValues
        }
        
        var user_obj = {
            user_name: req.user_name,
            user_fullname: student_info.dataValues.user_fullname,
            user_major: student_info.dataValues.user_major,
            user_class: student_info.dataValues.user_class,
            status: student_topic_info.dataValues.student_status
        }

        res.json({
            code: 200,
            data: {
                user_info: user_obj,
                topic_info: topic_info
            }
        })
        utils.create_a_log(req, '学生入所', '浏览详情')
     
    }
    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '浏览详情', '失败')
        return
    }
}

module.exports = get_details